package com.daqian.chance.core.rest;

import com.daqian.chance.core.enums.ErrorCode;
import lombok.Data;

@Data
public class RestResult<T> {

    public static final int OK = ErrorCode.OK.code();

    public static final String OK_MSG = "ok";

    public static final int ERR = ErrorCode.ERR.code();

    public static final String ERR_MSG = "未知异常";


    private int code;

    private String msg;

    private T data;

    /**
     * 构建响应消息
     * @param code    响应代码
     * @param msgInfo 响应说明
     * @param data    数据
     * @return
     */
    private static <T> RestResult<T> msg(int code, String msgInfo, T data) {
        RestResult restResult = new RestResult();
        restResult.setCode(code);
        restResult.setMsg(msgInfo == null ? ERR_MSG : msgInfo);
        restResult.setData(data);
        return restResult;
    }

    /**
     * 成功消息
     * @return
     */
    public static <T> RestResult<T> ok() {
        return msg(OK, OK_MSG, null);
    }

    /**
     * 成功消息
     * @param data
     * @return
     */
    public static <T> RestResult<T> ok(T data) {
        return msg(OK, OK_MSG, data);
    }

    /**
     * 成功消息
     * @param data
     * @return
     */
    public static <T> RestResult<T> ok(String msgInfo, T data) {
        return msg(OK, msgInfo, data);
    }


    /**
     * 失败消息
     * @return
     */
    public static <T> RestResult<T> fail() {
        return msg(ERR, ERR_MSG, null);
    }

    /**
     * 失败消息
     * @param code
     * @return
     */
    public static <T> RestResult<T> fail(int code) {
        return msg(code, ERR_MSG, null);
    }

    /**
     * 失败消息
     * @param reason
     * @return
     */
    public static <T> RestResult<T> fail(String reason) {
        return msg(ERR, reason, null);
    }

    /**
     * 失败消息
     * @param code
     * @param reason
     * @return
     */
    public static <T> RestResult<T> fail(int code, String reason) {
        return msg(code, reason, null);
    }

    /**
     * 失败消息
     * @param code
     * @param reason
     * @param data
     * @return
     */
    public static <T> RestResult<T> fail(int code, String reason, T data) {
        return msg(code, reason, data);
    }

}
